#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "key.h"

static volatile uint8_t last_pressed_key = 0;  // 记录最后按下的按键

void Key_EXTI_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;
    EXTI_InitTypeDef EXTI_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    
    // 1. 使能时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
    
    // 2. 配置PA0-PA3为上拉输入（低电平有效）
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    // 3. 配置EXTI线路
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); // PA0 -> EXTI0
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1); // PA1 -> EXTI1
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2); // PA2 -> EXTI2
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3); // PA3 -> EXTI3
    
    // 4. 统一配置EXTI参数
    EXTI_InitStruct.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
    EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; // 下降沿触发
    EXTI_InitStruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStruct);
    
    // 5. 配置NVIC（优先级分组2）
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    // EXTI0中断配置
    NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    
    // EXTI1中断配置
    NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
	
    // EXTI2中断配置
    NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    // EXTI3中断配置
    NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
}


// PA0中断
void EXTI0_IRQHandler(void) 
{
    if(EXTI_GetITStatus(EXTI_Line0) != RESET) 
	{
        last_pressed_key = KEY4;
        EXTI_ClearITPendingBit(EXTI_Line0);
    }
}

// PA1中断
void EXTI1_IRQHandler(void) 
{
    if(EXTI_GetITStatus(EXTI_Line1) != RESET) 
	{
        last_pressed_key = KEY3;
        EXTI_ClearITPendingBit(EXTI_Line1);
    }
}

// PA2中断
void EXTI2_IRQHandler(void) 
{
    if(EXTI_GetITStatus(EXTI_Line2) != RESET) 
	{
        last_pressed_key = KEY2;
        EXTI_ClearITPendingBit(EXTI_Line2);
    }
}

// PA3中断
void EXTI3_IRQHandler(void) 
{
    if(EXTI_GetITStatus(EXTI_Line3) != RESET) 
	{
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
		{
			Delay_ms(20);
			while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0);
			Delay_ms(20);
		}			
        last_pressed_key = KEY1;
        EXTI_ClearITPendingBit(EXTI_Line3);
    }
}

// 获取按键状态的函数
uint8_t Key_GetPressed(void) 
{
    uint8_t temp = last_pressed_key;
    last_pressed_key = 0;  // 读取后清零
    return temp;
}
